import * as lib from '../../../../../components/aotoo/lib/index';
import {getYmd, lunar, diffDay} from '../../../../../components/modules/calendar/index'
import { getCloudUser, getCloudFiles } from '../../cloud/index'

const commonHooks = lib.hooks('calender-announce', true)
const app = getApp()

// 分享弹窗数据存储名
const namespaceOfShareHooks = app.globalData.namespaceOfShareHooks
const preNamespaceOfShareHooks = app.globalData.pre_namespaceOfShareHooks

/**
 * 
 * onShareAppMessage
 * @param {} param 
 * @returns 
 */
export function onShareAppMessage(param){
  // modalShare.messageHint
  // modalShare.messageBody
  // modalShare.messageDate
  // modalShare.userInfo
  const messageHint = wx.modalShare.messageHint
  const user = wx.modalShare.userInfo
  const date = wx.modalShare.messageDate;
  const aryBuffer = unescape(encodeURIComponent(wx.modalShare.messageBody)).split("").map(val => val.charCodeAt());
  const content = wx.arrayBufferToBase64(aryBuffer)
  const shareId = lib.uuid(null, 6)
  const appPath = `pages/index/index?date=${date.timestr}&type=share&sid=${shareId}&from=${user.UID}&hint=${messageHint}&content=${content}`
  // console.log(appPath);
  return {
    title: wx.modalShare.messageHint,
    path: appPath
  }
}

/**
 * shareInpageOnload
 * 接收别人分享的信息，用于首页 page.onLoad
 */
export default function(param){
  const today = getYmd()

  preNamespaceOfShareHooks.forEach(prename=>{
    const shareInfoBack = commonHooks.getItem(prename)
    if (shareInfoBack) {
      commonHooks.removeItem(prename)
    }
  })

  let localShareInfo = commonHooks.getItem(namespaceOfShareHooks)
  if (localShareInfo) {
    if (!localShareInfo.data) {
      localShareInfo = null
      commonHooks.removeItem(namespaceOfShareHooks)
    } else {
      this.shareInfo = localShareInfo
    }
  }

  if (param) {
    if (param.date && param.type==='share' && param.content) {
      this.lazyOpenShareModal = true
      getCloudUser().then(profile=>{
        const {UID} = profile;
        app.globalData.userProfile = profile;
        if (param.from === UID) {
          this.lazyOpenShareModal = false
          this.openShareModal()
          return 
        }
        else {
          const {sid} = param
          const date = getYmd(param.date)
          if (date.timestamp >= today.timestamp || date.timestr === today.timestr) {
            if (localShareInfo) {
              const shareData = localShareInfo.data
              const hasItem = shareData.filter(share=>share.sid === sid)
              if (!hasItem.length) {
                localShareInfo.data = [param].concat(localShareInfo.data)
                this.shareInfo = localShareInfo
                commonHooks.setItem(namespaceOfShareHooks, localShareInfo)
              }
            } else {
              this.shareInfo = {data: [param]}
              commonHooks.setItem(namespaceOfShareHooks, {data: [param]})
            }
          }
          this.lazyOpenShareModal = false
          this.openShareModal()
        }
      })
    } 
    // else {

    // }
  } 
  // else {

  // }
}